package com.yuhao.servicea.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yuhao.servicea.enums.ResultEnum;
import com.yuhao.servicea.exception.ServiceException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
@RequestMapping("/hystrix")
public class HystrixController {
//      超时配置
//    @HystrixCommand(fallbackMethod = "fallback", commandProperties = {@HystrixProperty(
//            name = "execution.isolation.thread.timeoutInMilliseconds",
//            value = "3000"
//    )})
    //可以写入application.yml中前面要加 hystrix:command:default
    @HystrixCommand(fallbackMethod = "fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //设置熔断
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //请求数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //时间窗口， 休眠时间窗 半开  关闭 打开
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") //错误百分比条件  比如10次调用7次异常就是10次
    })
    @GetMapping("/get")
    public String get() {
        throw new ServiceException(ResultEnum.EXCEPTION);
    }

    @GetMapping("/get1")
    @HystrixCommand
    public String get1(){
        throw new ServiceException(ResultEnum.EXCEPTION);
    }

    private String fallback() {
        return "太拥挤了，请再次尝试";
    }

    public String defaultFallback() {
        return "defaultFallback";
    }
}
